package com.zxy.fmmall.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zxy.fmmall.entity.Category;
import com.zxy.fmmall.entity.ShoppingCart;
import com.zxy.fmmall.entity.Users;
import com.zxy.fmmall.service.ShoppingCartService;
import com.zxy.fmmall.utils.Base64Utils;
import com.zxy.fmmall.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;


@RestController
@CrossOrigin
@Api(value = "提供购物车管理接口", tags = "购物车管理")
@RequestMapping("/shopcart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private ObjectMapper objectMapper;

    @PostMapping("/add")
    @ApiOperation("购物车接口")
    public ResultVo listCart(@RequestBody ShoppingCart cart,@RequestHeader("token") String token) throws JsonProcessingException {
        String s = stringRedisTemplate.boundValueOps(token).get();
        Users users = objectMapper.readValue(s, Users.class);
        return shoppingCartService.addShopingCart(cart);
    }

    @GetMapping("/list")
    @ApiOperation("显示购物车列表接口")
    public ResultVo listShoppingCart(String userId) {
        return shoppingCartService.listShoppingCart(userId);
    }

    @PutMapping("/update/{cartId}/{cartNum}")
    @ApiOperation("更改购物车商品数量接口")
    public ResultVo updateCartNumByCartId(@PathVariable("cartId") String cartId,
                                          @PathVariable("cartNum") String cartNum) {
        return shoppingCartService.updateCartNumByCartId(cartId, cartNum);
    }

    @GetMapping("/listSelectCart/{cartId}")
    @ApiOperation("显示你所选择的物品表接口")
    public ResultVo listSelectCart(@PathVariable("cartId") String cartId) {
        return shoppingCartService.listShoppingCartByCartId(cartId);
    }
}
